package com.fer.controller;

import com.fer.model.Shop;
import com.fer.service.ShopService;
import com.fer.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;


    //查询所有店铺的信息
    @GetMapping
    @ResponseBody
    public Map<String,Object>  getAllShop(){
        Map<String,Object> result = new HashMap<>();
        List<Shop> shopList = shopService.selectAllShop();
        System.out.println(shopList);
        if(shopList.size()==0){
            result.put("code",400);
            result.put("message","查找失败");
            return result;
        }
        result.put("data",shopList);
        result.put("code",200);
        result.put("message","查找成功");
        return result;
    }

    @GetMapping("/{boss_id}")
    @ResponseBody
    //通过id查询单个店铺信息
    public Map<String,Object>  getShop(@PathVariable("boss_id") Integer boss_id){
//        System.out.println("controller执行了");
        Map<String,Object> result = new HashMap<>();
        Shop shopList = shopService.selectShop(boss_id);
       // System.out.println(shopList);
        if(shopList==null){
            result.put("code",200);
//            result.put("message","查找失败");
            result.put("data",shopList);
            return result;
        }
        result.put("code",200);
        result.put("message","查找成功");
        result.put("data",shopList);
        return result;
    }

    //增加一个店铺
    @PostMapping
    @ResponseBody
    public Map<String,Object>  addShop(Shop shop){
        Map<String,Object> result = new HashMap<>();
        try{
            shopService.insertShop(shop);
        }catch (Exception e){
            result.put("code",400);
            result.put("message","添加失败");
            return result;
        }
        result.put("code",200);
        result.put("message","添加成功");
        return result;
    }

    //根据id删店铺
    @DeleteMapping("/{shop_id}")
    @ResponseBody
    public Map<String,Object>  deleteShop(@PathVariable("shop_id") Integer shop_id){
        Map<String,Object> result = new HashMap<>();
        try{
            shopService.deleteShop(shop_id);
        }catch (Exception e){
            result.put("message","删除失败");
            return result;
        }
        result.put("message","删除成功2");
        return result;
    }


    //修改店铺信息
    @PutMapping
    @ResponseBody
    public Map<String,Object>  updateShop(Shop shop){
        Map<String,Object> result = new HashMap<>();
        boolean state = shopService.updateShop(shop);
        if (!state){
            result.put("message","修改失败");
            result.put("code",400);
            return result;
        }
        result.put("message","修改成功");
        result.put("code",200);
        return result;
    }

}
